<?php
class AuthorBehavior extends CActiveRecordBehavior
{
    public $createdByAttribute = 'created_by';
    public $updatedByAttribute = 'updated_by';
    
    public function beforeSave()
    {
        if ($this->getOwner()->getIsNewRecord())
        {
            if ($this->getOwner()->hasAttribute($this->createdByAttribute))
            {
                $this->getOwner()->{$this->createdByAttribute} = Yii::app()->user->id;
            }
        }
        if ($this->getOwner()->hasAttribute($this->updatedByAttribute))
        {
            $this->getOwner()->{$this->updatedByAttribute} = Yii::app()->user->id;
        }
    }
}